package com.haidechizi.sort;

import java.util.Arrays;

/**
 * @author: wangdejun
 * @create: 2020-01-10 14:56
 **/
public class ShellSort {

    public static void main(String[] args) {
        int[] arr = new int[]{5, 9, 3, 0, 8, 2, 7, 6, 5, 9, 6, 8, 4};
        System.out.println(Arrays.toString(arr));
        shellSort(arr);
        System.out.println(Arrays.toString(arr));
    }

    public static void shellSort(int[] arr) {
        // 根据数组的长度确定每次的步长
        for (int d = arr.length / 2; d > 0; d = d / 2) {
            for (int i = d; i < arr.length; i++) {
                for (int j = i - d; j >= 0; j = j - d) {
                    if (arr[j] > arr[j + d]) {
                        int temp = arr[j];
                        arr[j] = arr[j + d];
                        arr[j + d] = temp;
                    }
                }

            }
        }

    }
}
